package com.hnevc.recyclerviewdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/11/24.
 */

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    String[] datas ;//传递过来的文件数据
    TypedArray images;//传递过来的图片数据
    Context mContext;//传递过来的上下对象

    public MyAdapter(String[] datas, TypedArray images, Context mContext) {
        this.datas = datas;
        this.images = images;
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, null);
        MyViewHolder ViewHolder = new MyViewHolder(itemView);

        return ViewHolder;
    }
    //根据每一项数据，确定每一个列表显示的外观
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.textView.setText(datas[position]);
        //myViewHolder.imageView.setImageDrawable(images.getDrawable(position));
        myViewHolder.imageView.setImageResource(R.drawable.a1);
    }

    //返回RecyclerView控件需要显示的数据项数量
    @Override
    public int getItemCount() {
        return datas.length;
    }
    //创建一个RecyclerView的内部类
    class MyViewHolder extends  RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.id_iv_pic);
            textView = (TextView) itemView.findViewById(R.id.id_tv_datas);

        }
    }
}
